package collections;

/**
 * @Description: 包装类型转换工具类
 * @author: fengrun@pinduoduo.com
 * @date: 2018/9/25
 */

public class WrapperUtils {

    private static final String TAG = "ConvertVoUtils";

    public static boolean toBoolean(Boolean v) {
        return toBoolean(v, false);
    }

    public static boolean toBoolean(Boolean v, boolean defVal) {
        if (v == null) {
            return defVal;
        }
        return v;
    }

    public static boolean convertBoolean(String str) {
        return convertBoolean(str, false);
    }

    public static boolean convertBoolean(String str, boolean defVal) {

        try {
            return Boolean.parseBoolean(str);
        } catch (Exception e) {
            return defVal;
        }
    }

    public static byte toByte(Byte b) {
        return toByte(b, (byte) 0);
    }

    public static byte toByte(Byte v, byte defVal) {
        if (v == null) {
            return defVal;
        }
        return v;
    }

    public static byte convertByte(String str) {
        return convertByte(str, (byte) 0);
    }

    public static byte convertByte(String str, byte defVal) {

        try {
            return Byte.parseByte(str);
        } catch (Exception e) {
            return defVal;
        }
    }

    public static short toShort(Short v) {
        return toShort(v, (short) 0);
    }

    public static short toShort(Short v, short defVal) {
        if (v == null) {
            return defVal;
        }
        return v;
    }

    public static short convertShort(String str) {
        return convertShort(str, (short) 0);
    }

    public static short convertShort(String str, short defVal) {

        try {
            return Short.parseShort(str);
        } catch (Exception e) {
            return defVal;
        }
    }

    public static int toInt(Integer i) {
        return toInt(i, 0);
    }

    public static int toInt(Integer i, int defVal) {
        if (i == null) {
            return defVal;
        }
        return i;
    }

    public static int convertInt(String str) {
        return convertInt(str, 0);
    }

    public static int convertInt(String str, int defVal) {

        try {
            return Integer.parseInt(str);
        } catch (Exception e) {
            return defVal;
        }
    }

    public static long toLong(Long v) {
        return toLong(v, 0L);
    }

    public static long toLong(Long v, long defVal) {
        if (v == null) {
            return defVal;
        }
        return v;
    }

    public static long convertLong(String str) {
        return convertLong(str, 0);
    }

    public static long convertLong(String str, long def) {

        try {
            return Long.parseLong(str);
        } catch (Exception e) {
            return def;
        }
    }

    public static float toFloat(Float v) {
        return toFloat(v, 0L);
    }

    public static float toFloat(Float v, float defVal) {
        if (v == null) {
            return defVal;
        }
        return v;
    }

    public static float convertFloat(String str) {
        return convertFloat(str, 0);
    }

    public static float convertFloat(String str, float defVal) {

        try {
            return Float.parseFloat(str);
        } catch (Exception e) {
            return defVal;
        }
    }

    public static double toDouble(Double v) {
        return toDouble(v, 0D);
    }

    public static double toDouble(Double v, double defVal) {
        if (v == null) {
            return defVal;
        }
        return v;
    }

    public static double convertDouble(String str) {
        return convertDouble(str, 0);
    }

    public static double convertDouble(String str, double defVal) {

        try {
            return Double.parseDouble(str);
        } catch (Exception e) {
            return defVal;
        }
    }
}
